/**
 * Created by wzh on 2016/7/9.
 */
'use strict'
swan_pc
    .directive('searchAll',function(ContactInfo,CompanyInfo,OppInfo){
        return {
            restrict: 'AE',
            replace:true,
            templateUrl:"scripts/services/directives/wzh/search/search.html",
            scope: {
                resultArray:"=",
                singleResult:"=",
                option:"@",
                searchClass:"@",
                submitFunction:"&",
                single:"@",
                isShow:"@",
                companyFilter:"="
            },
            controller:['$scope', function($scope){
                $scope.result;
                $scope.init=function() {
                    if($scope.singleResult!=undefined&&$scope.singleResult.id!=undefined&&$scope.singleResult.id!="")
                        $scope.resultArray[$scope.singleResult.id]=($scope.option=="contact")?$scope.singleResult.baseInfo.name:($scope.option=="company")?$scope.singleResult.companyBaseInfo.name:($scope.option=="opportunity")?$scope.singleResult.opportunity.oppName:"";
                    $scope.useCompanyFilter=$scope.companyFilter!=undefined&&$scope.companyFilter!=""?true:false;
                    $scope.containChild=false;
                    if ($scope.option == "contact") {
                        ContactInfo.query(function (result) {
                            $scope.result = result;
                        })
                    }
                    else if ($scope.option == "company") {
                        CompanyInfo.query({infoid:"all"},function (result) {
                            for(var i=0;i<result.length;i++){
                                if(result[i].subCompanys!=undefined&&result[i].subCompanys.length>0){
                                    for(var j=0;j<result[i].subCompanys.length;j++){
                                        result.push(result[i].subCompanys[j]);
                                    }
                                }
                            }
                            $scope.result = result;
                        })
                    }
                    else if ($scope.option = "oppotunity") {
                        OppInfo.query({infoid:"all"},function(result) {
                            $scope.result=result;
                        })
                    }
                    $scope.filterInfos={
                        name:"",
                        one:{
                            a:false,
                            b:false,
                            c:false,
                            d:false,
                            e:false
                        },
                        two:{
                            a:false,
                            b:false,
                            c:false,
                            d:false,
                            e:false
                        },
                        three:{
                            a:false,
                            b:false,
                            c:false,
                            d:false,
                            e:false
                        },
                        four:{
                            a:false,
                            b:false,
                            c:false,
                            d:false,
                            e:false
                        }
                    };
                }
                $scope.init();
                if($scope.resultArray==undefined) $scope.resultArray={};
                Object.prototype.toString=function (){
                    var keys=Object.keys($scope.resultArray);
                    var str=""
                    for (var i=0;i<keys.length;i++){
                        if(keys[i]=="toString") continue;
                        if (i!=0) str+=",";
                        str+=keys[i];
                        str+=":";
                        str+=$scope.resultArray[keys[i]];
                    }
                    return str;
                }
                $scope.initAll=function(){
                    $scope.resultArray={};
                    $scope.init();
                }
                $scope.checked={
                    color:"green"
                }
                $scope.unchecked={
                    color:"black"
                }
                $scope.pushItem=function(item){
                    if($scope.single=="true"){
                        $scope.resultArray={};
                        $scope.singleResult=item;
                    }
                    $scope.resultArray[item.id]=($scope.option=="contact")?item.baseInfo.name:($scope.option=="company")?item.companyBaseInfo.name:($scope.option=="opportunity")?item.opportunity.oppName:"";
                }
                $scope.popItem=function(id){
                    if($scope.single=="true") return;
                    delete $scope.resultArray[id];
                }
                function resursionCheckContactCompany(name,company){
                    if (name==company.companyBaseInfo.name) return true;
                    else if(company.subCompanys.length>0){
                        for (var i=0;i<company.subCompanys.length;i++){
                            if (resursionCheckContactCompany(name,company.subCompanys[i])) return true;
                        }
                    }
                    return false;
                }
                $scope.checkContactCompany=function(name){
                    if ($scope.containChild==false) return name==$scope.companyFilter.companyBaseInfo.name;
                    else return resursionCheckContactCompany(name,$scope.companyFilter);
                }
                //筛选函数，
                $scope.infoFilter=function(item){
                    //联系人
                    if($scope.option=="contact") {
                        if (item.baseInfo.name.indexOf($scope.filterInfos.name) < 0) return false;
                        if($scope.useCompanyFilter&&!$scope.checkContactCompany(item.userCompanyName)) return false;
                        if (!(!$scope.filterInfos.one.a && !$scope.filterInfos.one.b && !$scope.filterInfos.one.c && !$scope.filterInfos.one.d && !$scope.filterInfos.one.e
                            || item.personalityInfo != undefined && (
                            $scope.filterInfos.one.a && item.personalityInfo.businessConnection == "客户" ||
                            $scope.filterInfos.one.b && item.personalityInfo.businessConnection == "合作伙伴" ||
                            $scope.filterInfos.one.c && item.personalityInfo.businessConnection == "竞争对手" ||
                            $scope.filterInfos.one.d && item.personalityInfo.businessConnection == "供应商" ||
                            $scope.filterInfos.one.e && item.personalityInfo.businessConnection == "其他"))) return false;
                        if (!(!$scope.filterInfos.two.a && !$scope.filterInfos.two.b && !$scope.filterInfos.two.c && !$scope.filterInfos.two.d && !$scope.filterInfos.two.e
                            || item.personalityInfo != undefined && (
                            $scope.filterInfos.two.a && item.personalityInfo.importanceToMe == "不需要" ||
                            $scope.filterInfos.two.b && item.personalityInfo.importanceToMe == "需要" ||
                            $scope.filterInfos.two.c && item.personalityInfo.importanceToMe == "重要" ||
                            $scope.filterInfos.two.d && item.personalityInfo.importanceToMe == "很重要" ||
                            $scope.filterInfos.two.e && item.personalityInfo.importanceToMe == "极其重要"))) return false;
                        if (!(!$scope.filterInfos.three.a && !$scope.filterInfos.three.b && !$scope.filterInfos.three.c && !$scope.filterInfos.three.d && !$scope.filterInfos.three.e
                            || item.personalityInfo != undefined && (
                            $scope.filterInfos.three.a && item.personalityInfo.friendshipWithMe == "讨厌" ||
                            $scope.filterInfos.three.b && item.personalityInfo.friendshipWithMe == "烦" ||
                            $scope.filterInfos.three.c && item.personalityInfo.friendshipWithMe == "认识" ||
                            $scope.filterInfos.three.d && item.personalityInfo.friendshipWithMe == "接受" ||
                            $scope.filterInfos.three.e && item.personalityInfo.friendshipWithMe == "好感"))) return false;
                        return true;
                    }
                    //公司
                    if($scope.option=="company") {
                        if (item.companyBaseInfo.name.indexOf($scope.filterInfos.name) < 0) return false;
                        if (!(!$scope.filterInfos.one.a && !$scope.filterInfos.one.b && !$scope.filterInfos.one.c && !$scope.filterInfos.one.d && !$scope.filterInfos.one.e
                            || item.businessConnectionWithMe != undefined && (
                            $scope.filterInfos.one.a && item.businessConnectionWithMe == "客户" ||
                            $scope.filterInfos.one.b && item.businessConnectionWithMe == "合作伙伴" ||
                            $scope.filterInfos.one.c && item.businessConnectionWithMe == "竞争对手" ||
                            $scope.filterInfos.one.d && item.businessConnectionWithMe == "供应商" ||
                            $scope.filterInfos.one.e && item.businessConnectionWithMe == "其他"))) return false;
                        if (!(!$scope.filterInfos.two.a && !$scope.filterInfos.two.b && !$scope.filterInfos.two.c && !$scope.filterInfos.two.d && !$scope.filterInfos.two.e
                            || item.importanceToMe != undefined && (
                            $scope.filterInfos.two.a && item.importanceToMe == "不需要" ||
                            $scope.filterInfos.two.b && item.importanceToMe == "需要" ||
                            $scope.filterInfos.two.c && item.importanceToMe == "重要" ||
                            $scope.filterInfos.two.d && item.importanceToMe== "很重要" ||
                            $scope.filterInfos.two.e && item.importanceToMe == "极其重要"))) return false;
                        if (!(!$scope.filterInfos.three.a && !$scope.filterInfos.three.b && !$scope.filterInfos.three.c && !$scope.filterInfos.three.d && !$scope.filterInfos.three.e
                            || item.personalityInfo != undefined && (
                            $scope.filterInfos.three.a && item.friendshipWithMe == "讨厌" ||
                            $scope.filterInfos.three.b && item.friendshipWithMe == "烦" ||
                            $scope.filterInfos.three.c && item.friendshipWithMe == "认识" ||
                            $scope.filterInfos.three.d && item.friendshipWithMe== "接受" ||
                            $scope.filterInfos.three.e && item.friendshipWithMe == "好感"))) return false;
                        return true;
                    }
                    if($scope.option=="opportunity") {
                        if (item.opportunity.oppName.indexOf($scope.filterInfos.name) < 0) return false;
                        if (!(!$scope.filterInfos.one.a && !$scope.filterInfos.one.b && !$scope.filterInfos.one.c && !$scope.filterInfos.one.d && !$scope.filterInfos.one.e
                            || item.opportunity != undefined && (
                            $scope.filterInfos.one.a && item.opportunity.reality == "LOST" ||
                            $scope.filterInfos.one.b && item.opportunity.reality == "WIN" ||
                            $scope.filterInfos.one.c && item.opportunity.reality == "OPEN" ||
                            $scope.filterInfos.one.d && item.opportunity.reality == "" ||
                            $scope.filterInfos.one.e && item.opportunity.reality == ""))) return false;
                        if (!(!$scope.filterInfos.two.a && !$scope.filterInfos.two.b && !$scope.filterInfos.two.c && !$scope.filterInfos.two.d && !$scope.filterInfos.two.e
                            || item.opportunity != undefined && (
                            $scope.filterInfos.two.a||
                            parseInt($scope.filterInfos.two.b && item.opportunity.odds.substring(0,2))>59||
                            parseInt($scope.filterInfos.two.c && item.opportunity.odds.substring(0,2))>79))) return false;
                        if (!(!$scope.filterInfos.three.a && !$scope.filterInfos.three.b && !$scope.filterInfos.three.c && !$scope.filterInfos.three.d && !$scope.filterInfos.three.e
                            || item.opportunity != undefined && (
                            $scope.filterInfos.three.a && item.opportunity.controllerType == "我自己" ||
                            $scope.filterInfos.three.b && item.opportunity.controllerType == "公司" ||
                            $scope.filterInfos.three.c  ))) return false;
                        if (!(!$scope.filterInfos.four.a && !$scope.filterInfos.four.b && !$scope.filterInfos.four.c && !$scope.filterInfos.four.d && !$scope.filterInfos.four.e
                            || item.opportunity != undefined && (
                            $scope.filterInfos.four.a && item.opportunity.controllerType == "我自己" ||
                            $scope.filterInfos.four.b && item.opportunity.controllerType == "公司"  ))) return false;
                        return true;
                    }
                }
            }]
        }
    })
